import com.alibaba.fastjson.JSONObject;
import com.binguo.api.response.api.BaseChargeResp;
import com.binguo.api.response.api.BaseUserInfo;
import com.binguo.api.service.ApiOrderService;
import com.binguo.api.service.api.ShopApiInterface;
import com.binguo.common.enums.PayStatus;
import com.binguo.common.enums.PayType;
import com.binguo.common.exception.ServiceException;
import com.binguo.model.api.dto.OrderDTO;
import com.binguo.model.api.vo.OrderVO;
import com.binguo.model.model.ShopSaobeiConfig;
import com.binguo.model.repository.ShopSaobeiConfigRepository;
import com.binguo.thirdPay.saoBeiPay.SaoBeiPayService;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author yuritian
 * @Title: SaoBeiService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020/6/2313:48
 */
public class SaoBeiService implements ShopApiInterface {

    @Resource
    private SaoBeiPayService saoBeiPayService;

    @Resource
    private ShopSaobeiConfigRepository shopSaobeiConfigRepository;

    @Resource
    private ApiOrderService apiOrderService;

    @Override
    public BaseChargeResp payPreHandler(OrderDTO orderDTO) {
        if (orderDTO.getPayType().equals(PayType.MICROPAY)){
            return qrCodePay(orderDTO);
        }else {
            throw new ServiceException("不支持支付方式");
        }
    }

    @Override
    public BaseChargeResp payPostHandler(OrderDTO orderDTO) {
        return null;
    }

    @Override
    public BaseUserInfo getUserInfo(String cardNo) {
        return null;
    }

    @Override
    public BaseChargeResp getProductList() {
        return null;
    }

    @Override
    public BaseChargeResp cardPay(OrderDTO orderDTO) {
        return null;
    }

    @Override
    public BaseChargeResp wxPay(OrderDTO orderDTO) {
        return null;
    }

    @Override
    public BaseChargeResp aliPay(OrderDTO orderDTO) {
        return null;
    }

    @Override
    public BaseChargeResp facePay(OrderDTO orderDTO) {
        return null;
    }

    @Override
    public BaseChargeResp cashPay(OrderDTO orderDTO) {
        return null;
    }

    @Override
    public BaseChargeResp qrCodePay(OrderDTO orderDTO) {

        BaseChargeResp<OrderVO> result = new BaseChargeResp<>();

        Condition condition = new Condition(ShopSaobeiConfig.class);
        condition.or().andEqualTo("shopId",orderDTO.getShopId());
        List<ShopSaobeiConfig> configList = shopSaobeiConfigRepository.findByCondition(condition);

        if (null == configList || configList.size() == 0){
            throw new ServiceException("该商户没有扫呗配置,支付失败");
        }
        ShopSaobeiConfig config = configList.get(0);

        Date date = new Date();
        orderDTO.setPayTime(date);

        JSONObject payResult = saoBeiPayService.qrcodePay(config.getMerchantNo(),config.getTerminalId(),orderDTO.getOrderNo(),orderDTO.getAuthCode(),orderDTO.getRealFee().toString(),date);

        String payType = payResult.getString("payType");

        if (payType.equals("010")){
            orderDTO.setNote("010微信支付");
        }else if (payType.equals("020")){
            orderDTO.setNote("020支付宝支付");
        }else if (payType.equals("060")){
            orderDTO.setNote("060qq钱包支付");
        }else if (payType.equals("080")){
            orderDTO.setNote("080京东钱包支付");
        }else if (payType.equals("090")){
            orderDTO.setNote("090口碑支付");
        }else if (payType.equals("100")){
            orderDTO.setNote("100翼支付");
        }else if (payType.equals("110")){
            orderDTO.setNote("110银联二维码");
        }else if (payType.equals("140")){
            orderDTO.setNote("140和包支付（仅限和包通道)");
        }else {
            orderDTO.setNote(payType);
        }

        String outTradeNo = payResult.getString("outTradeNo");

        OrderVO orderVO = apiOrderService._createOrder(orderDTO, PayStatus.SUCCESS,outTradeNo,null);

        result.setData(orderVO);
        result.setCode(200);
        return result;
    }
}